var Colors = require('Colors');
var Global = require('Global');

cc.Class({
    extends: cc.Component,

    properties: {
        numLabel:{
            default:null,
            type:cc.Label,
        },
        clickEffect:cc.AudioClip,
        addCoin:cc.AudioClip,
    },

    onLoad(){
        this.node.on(cc.Node.EventType.TOUCH_START,function(event){
            if(!this.game.isCal){
                cc.audioEngine.playEffect(this.clickEffect);
                this.game.isCal = true;
                Global.combo = 0;
                cc.audioEngine.playEffect(this.addCoin);
                this.setNum(parseInt(this.numLabel.string)+1,true,false);
            }
        },this);
    },

    setArrPosition(row,col){
        this.row = row;
        this.col = col;
        let posArr = Global.pos(row,col);
        this.node.setPosition(posArr[0],posArr[1]);
    },

    destoryTile(){
        var action = cc.sequence(cc.scaleTo(0.1,0),cc.callFunc(function(node){
            node.destroy();
        },this.node,this.node));
        this.node.runAction(action);
    },

    newTile(row,col){
        this.node.setScale(0);
        this.node.runAction(cc.scaleTo(0.1,1));
        this.setArrPosition(row,col);
    },

    moveTo(row,col){
        this.row = row;
        this.col = col;
        this.node.stopActionByTag(1);
        let posArr = Global.pos(row,col);
        var action = cc.moveTo(0.2,cc.p(posArr[0],posArr[1]));
        this.node.runAction(action);
        action.setTag(1);
    },

    setNum(num,exeLogic,playEffect){
        this.game.maxNum = num > this.game.maxNum ? num : this.game.maxNum;
        this.numLabel.string = num;
        this.node.color = Colors.numBg[num - 1];
        if(playEffect){
            this.node.runAction(cc.sequence(cc.scaleTo(0.15,1.5),cc.scaleTo(0.15,1)));
        }
        if(exeLogic){
            var isMove = this.game.operateLogic(this.row,this.col,parseInt(this.numLabel.string),true);
            var powers = this.game.powers;
            console.info('--------'+isMove);
            if(!isMove){
                Global.powerNum--;
                powers[Global.powerNum].color = Colors.powerBarBg;
                // powers[powerNum].runAction(cc.tintTo(Colors.powerBarBg));
            }
            if(Global.powerNum == 0){
                Global.score = this.game.scoreNum.string;
                //GameOver
                this.game.gameOver();
            }
        }
    }

});
